# -*- coding: utf-8 -*-
import sqlite3
conn = sqlite3.connect(r'data\car.sqlite')

# list 转 dict
# rows = cur.fetchall()
# rows2 = [dict(zip([desc[0] for desc in cur.description], row)) for row in rows]
# 用这种方式比较灵活,我觉得你可以写个转换函数,专门转换,不需要修改数据驱动源代码.

# 设置 text_factory

# by default, rows are returned as Unicode
# conn.text_factory = unicode

# but we can make sqlite3 always return bytestrings ...
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the
# database ...
# conn.text_factory = str

# we can also implement a custom text_factory ...
# here we implement one that will ignore Unicode characters that cannot be
# decoded from UTF-8
# conn.text_factory = lambda x: unicode(x, "utf-8", "ignore")

# sqlite3 offers a built-in optimized text_factory that will return bytestring
# objects, if the data is in ASCII only, and otherwise return unicode objects
# conn.text_factory = sqlite3.OptimizedUnicode

# 设置 row_factory
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
rs = cursor.execute(
    "SELECT carid,carlpn,substr(carvin,-6,6) as carvin,carclass,caremail,carowner,carmobile FROM car").fetchall()
print rs,type(rs)
for r in rs:
    print r.keys()
    l=[]
    for m in r:
        l.append(m)
        print m,type(m)
    print l
cursor.close()
conn.close()
